/**
 * 
 */
package src;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import java.util.Arrays;
import src.SMSConstant;

/**
 * @author Govind Kothari
 * 
 */
public class GetNewCluster {

	/**
	 * @param args
	 */
	public void ABC(){
		
		String [] array = {"rming","romg","roaming"};
		IdentifyClusterHead ich = new IdentifyClusterHead();
		Vector cluster = new Vector();
		Vector cluster_head= new Vector();

		for(int i=0;i< array.length; i++){
			cluster.add(array[i]);
		}
		cluster_head.add("roaming");
		
		
		Vector co_occur_right = ich.identifyCoOccur(cluster, SMSConstant.smsngram, 0, 1);
		Vector possible_head_right = ich.identifyCoOccur( cluster_head, SMSConstant.faqngram, 0, 1);
		
		Vector co_occur_left = ich.identifyCoOccur(cluster, SMSConstant.smsngram, 1, 0);
		Vector possible_head_left = ich.identifyCoOccur( cluster_head, SMSConstant.faqngram, 1, 0);
		
		divideCandidateSet(co_occur_left, possible_head_left);
				
	}
	public String searchDictionary(String token, Vector dictionary){
		for(int i=0; i< dictionary.size(); i++){
			if(dictionary.get(i).equals(token)){return token;}
		}		
		return null;
	}
	
	public String containsDictionaryWord(Vector tokenset){
		File file = new File(SMSConstant.faqngram);
		Vector domain_dictionary;
		String dictionary_word = null;
		domain_dictionary = new Vector();
		
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String sentence = null;
			while((sentence = reader.readLine())!= null){
				domain_dictionary.add(sentence);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		for(int i=0; i< tokenset.size(); i++){
			if((dictionary_word = searchDictionary((String)tokenset.get(i), domain_dictionary))!= null){				
				return dictionary_word;
			}		
		}		
		return null;
	}
	
	public void findCharacterStat(Vector tokenset){
		String token = null;
		char [] firstchar = new char [tokenset.size()];
		for(int i=0; i<tokenset.size(); i++){
			token = (String)tokenset.get(i);
			firstchar[i] = token.charAt(0);
		}
		Arrays.sort(firstchar);
		char maxchar = getMax(firstchar);
	}

	//-----------below method requires change--to get maximum start and end chars
	public char getMax(char [] firstchar){
		char max;
		int maxcount =0;
		int count=1;
		max = firstchar[0];
		for(int i=1;i< firstchar.length; i++){
			if(firstchar[i-1]== firstchar[i]){count++;}
			else{
				if(maxcount< count){maxcount = count; max = firstchar[i-1];}
				count =1;
			}
		}
		return max;
	}
	
	public void  divideCandidateSet(Vector tokenset, Vector wordset){
		String seedtoken = null;
		Vector cluster = new Vector();
		
		if((seedtoken = containsDictionaryWord(tokenset))!=null){
			tokenset.removeElement(seedtoken);
		}
		else{
			wordset.remove(seedtoken);
		}
		
		cluster.add(seedtoken);
		int tau = 3;
		
		for(int i=0; i<tokenset.size(); i++ ){
			if(SMSEditDistance(seedtoken,(String)tokenset.get(i))< tau){
				cluster.add(tokenset.get(i));
				tokenset.remove(i);
			}
		}
		
		for(int i=0; i< cluster.size(); i++){
			Debug.print((String)cluster.get(i));
		}		
	}

	public int SMSEditDistance(String seedtoken, String candidateToken){
		if(match(seedtoken,candidateToken)){return 0;}	
		else{
			return LevenshteinDistance.getLevenshteinDistance(seedtoken, candidateToken);
		}
	}
	
	public boolean match(String seedtoken, String candidateToken){
		String [] st1 = seedtoken.split("");
		String [] ct1 = candidateToken.split("");
		
		String [] cleanWord = new String [st1.length-1];
		String [] noisyToken = new String [ct1.length-1];
		
		for(int i=1; i<st1.length;i++){
			cleanWord[i-1]= st1[i];
		}
		for(int i=1; i<ct1.length;i++){
			noisyToken[i-1]= ct1[i];
		}

		int cleanIndex = 0;
		int noisyIndex = 0;
		
		while((cleanIndex < cleanWord.length)&&(noisyIndex < noisyToken.length)){
			
			if(cleanWord[cleanIndex].equals(noisyToken[noisyIndex])){cleanIndex++;noisyIndex++;}
			else if((isVowel(cleanWord[cleanIndex]))&&(isVowel(noisyToken[noisyIndex]))){
				cleanIndex++; noisyIndex++;
			}
			else if(isVowel(cleanWord[cleanIndex])){cleanIndex++;}
			else if((noisyIndex != 0)&&(cleanWord[cleanIndex].equals(noisyToken[noisyIndex-1]))){cleanIndex++;}
			else if(isDigit(noisyToken[noisyIndex])){
				
				if(noisyToken.length < 3){return false;}
				if(noisyIndex == noisyToken.length-1){
					if(noisyToken.length>2){return true;}
					else {return false;}
				}
				noisyIndex++;
				
				while(!cleanWord[cleanIndex].equals(noisyToken[noisyIndex])){
					//System.out.println(cleanWord[cleanIndex]+" "+noisyToken[noisyIndex]);
					cleanIndex++;
					if(cleanIndex == cleanWord.length ){return true;}
				}
			}
			else{
				if((cleanIndex > 0.7*cleanWord.length)&&(cleanWord.length>4)){return true;}
				else {return false;}
			}
		}
		return true;		
	}
	
	public boolean isVowel(String a){
		char c = a.charAt(0);
		if(c =='a'||c =='e'||c =='i'||c =='o'||c =='u'){return true;}
		else{return false;}
	}
	
	public boolean isDigit(String a){
		if(Character.isDigit(a.charAt(0))){return true;}
		else{return false;}
	}
	
	public static void main(String[] args) {
		GetNewCluster gc = new GetNewCluster();			
		gc.ABC();
		
	}
}
